package com.csii.pos.scalegrade.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csii.adhoc.exception.ParamValidityException;
import com.csii.adhoc.util.DateUtils;
import com.csii.pos.scalegrade.BusnScaleGrdSu;
import com.csii.pos.scalegrade.ScaleGradeDetailVO;
import com.csii.pos.scalegrade.mapper.BusnScaleGrdSuMapper;
import com.csii.pos.scalegrade.service.IBusnScaleGrdSuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Description: 业务规模档次设置
 * @Author: zhanghao
 * @Date:   2021-07-06
 * @Version: V1.0
 */
@Service
public class BusnScaleGrdSuServiceImpl extends ServiceImpl<BusnScaleGrdSuMapper, BusnScaleGrdSu> implements IBusnScaleGrdSuService {

    @Autowired
    private BusnScaleGrdSuMapper busnScaleGrdSuMapper;

    @Override
    public IPage<BusnScaleGrdSu> findAllList(Page page, BusnScaleGrdSu busnScaleGrdSu) {
        IPage<BusnScaleGrdSu> busnScaleGrdSuIPage = busnScaleGrdSuMapper.queryAllList(page, busnScaleGrdSu);
        //List<BusnScaleGrdSu> records = busnScaleGrdSuIPage.getRecords();

        return busnScaleGrdSuIPage;
    }

    @Override
    @Transactional
    public void removeByThreeCondition(List<BusnScaleGrdSu> busnScaleGrdSu) {
        if (busnScaleGrdSu == null || busnScaleGrdSu.size() == 0){
            throw new ParamValidityException("至少选中一个!");
        }
        busnScaleGrdSuMapper.deleteByThreeCondition(busnScaleGrdSu);
    }

    @Override
    public void updateByThreeCondition(BusnScaleGrdSu busnScaleGrdSu) {
        QueryWrapper<BusnScaleGrdSu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("SCALE_GRD_NO", busnScaleGrdSu.getScaleGrdNo());
        queryWrapper.eq("SCALE_GRD", busnScaleGrdSu.getScaleGrd());
        queryWrapper.eq("SCALE_TYPE", busnScaleGrdSu.getScaleType());
        busnScaleGrdSuMapper.update(busnScaleGrdSu, queryWrapper);
    }

    @Override
    public void addBusnScaleGrdSu(BusnScaleGrdSu busnScaleGrdSu) {
        //新增数据即是生效状态,故设置生效时间为当前时间和状态为生效状态"1"
        if (StringUtils.isEmpty(busnScaleGrdSu.getScaleGrdNo())){
            throw new ParamValidityException("ScaleGrdNo存量规模档次编号不能为空!");
        }
        if (StringUtils.isEmpty(busnScaleGrdSu.getScaleGrd().toString())) {
            throw new ParamValidityException("ScaleGrd规模档次不能为空!");
        }
        if (StringUtils.isEmpty(busnScaleGrdSu.getScaleDesc())) {
            throw new ParamValidityException("ScaleDesc规模描述不能为空!");
        }
        if (StringUtils.isEmpty(busnScaleGrdSu.getScaleGrdLl().toString())) {
            throw new ParamValidityException("ScaleGrdLl规模档次下限不能为空!");
        }
        if (StringUtils.isEmpty(busnScaleGrdSu.getScaleGrdUl().toString())) {
            throw new ParamValidityException("ScaleGrdLl规模档次下限不能为空!");
        }
        if (StringUtils.isEmpty(busnScaleGrdSu.getScaleGrdLl().toString())) {
            throw new ParamValidityException("ScaleGrdUl规模档次上限不能为空!");
        }
        if (StringUtils.isEmpty(busnScaleGrdSu.getScaleType())) {
            throw new ParamValidityException("ScaleType规模类型不能为空!");
        }

        /*if (busnScaleGrdSu.getScaleGrdUl() >= busnScaleGrdSu.getScaleGrdUl()) {
            throw new ParamValidityException("规模档次下线不能大于等于规模档次上线!");
        }*/

        String dateString = DateUtils.getDateString("yyyyMMdd");
        busnScaleGrdSu.setGrdTeDt(dateString);
        busnScaleGrdSuMapper.insert(busnScaleGrdSu);
    }

    @Override
    public String findScaleGrdNameById(BusnScaleGrdSu busnScaleGrdSu) {
        if (busnScaleGrdSu.getScaleGrdNo() == null || "".equals(busnScaleGrdSu.getScaleGrdNo())){
            throw new ParamValidityException("ScaleGrdNo不能为空!");
        }
        String scaleGrdName = busnScaleGrdSuMapper.queryScaleGrdNameById(busnScaleGrdSu);
        return scaleGrdName;
    }

    @Override
    public boolean isExistData(BusnScaleGrdSu busnScaleGrdSu) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("SCALE_GRD_NO", busnScaleGrdSu.getScaleGrdNo());
        queryWrapper.eq("SCALE_GRD", busnScaleGrdSu.getScaleGrd());
        queryWrapper.eq("SCALE_TYPE", busnScaleGrdSu.getScaleType());
        BusnScaleGrdSu one = busnScaleGrdSuMapper.selectOne(queryWrapper);
        boolean flag = false;
        if (one != null) {
            flag = true;
        }
        return flag;
    }

    @Override
    @Transactional
    public void isValidBatch(List<BusnScaleGrdSu> busnScaleGrdSusList) {
        busnScaleGrdSuMapper.updateBatch(busnScaleGrdSusList);
    }

}
